var express = require('express'),
    router = express.Router(),
    slugify = require('strman').slugify,
    pinyin = require('pinyin'),
    mongoose = require('mongoose'),
    Post = mongoose.model('Post');
    Category = mongoose.model('Category');

module.exports = function (app) {
  app.use('/admin/categories', router);
};

router.get('/', function (req, res, next) {
  res.render('admin/category/index', {
    title: '分类列表',
    pretty: true,
  });
});

router.get('/add', function (req, res, next) {
  res.render('admin/category/add', {
    action: "/admin/categories/add",
    title: '添加分类',
    pretty: true,
    category: { _id: '' },
  });
});

router.post('/add', function (req, res, next) {
  req.checkBody('name', '分类标题不能为空').notEmpty();

  var errors = req.validationErrors();
  if (errors) {
    console.log("errors: ");
    console.log(errors);
    return res.render('admin/category/add', {
           errors: errors,
           name: req.body.name,
    });
  }

  var name = req.body.name.trim();
  var py = pinyin(name, {
      style: pinyin.STYLE_NORMAL,
      heteronym: false
  }).map(function (item) {
    return item[0];
  }).join(' ');

  var slug = slugify(py);

  var category = new Category({
      name: name,
      slug: slug,
      created: new Date(),
  });

  category.save(function (err, category) {
    if (err) {
      req.flash('error', '分类保存失败');
      res.redirect('/admin/categories/add');
    } else {
      req.flash('info', '分类保存成功');
      res.redirect('/admin/categories');
    }      
  });

});

router.get('/edit/:id', getCategoryById, function (req, res, next) {
  res.render('admin/category/add', {
    title: '编辑分类',
    action: "/admin/categories/edit/" + req.category._id,
    category: req.category,
  });  
});

router.post('/edit/:id', getCategoryById, function (req, res, next) {
  var category = req.category;
  var name = req.body.name.trim();

  var py = pinyin(name, {
      style: pinyin.STYLE_NORMAL,
      heteronym: false
  }).map(function (item) {
    return item[0];
  }).join(' ');
  var slug = slugify(py);


  category.name = name;
  category.slug = slug;

  category.save(function (err, category) {
    if (err) {
      req.flash('error', '分类编辑失败');
      res.redirect('/admin/categories/edit' + category._id);
    } else {
      req.flash('info', '分类编辑成功');
      res.redirect('/admin/categories');
    }      
  });
})

router.get('/delete/:id', getCategoryById, function (req, res, next) {  
  req.category.remove(function (err, rowsRemoved) {
    if (err) {
      return next(err);
    }

    if (rowsRemoved) {
      req.flash('success', '分类删除成功');
    } else {
      req.flash('err', '分类删除失败');
    }

    res.redirect('/admin/categories');
  })
});

function getCategoryById(req, res, next) {
  if (!req.params.id) {
    return next(new Error('no category id provided'));
  }

  Category.findOne({ _id: req.params.id })
      .exec(function (err, category) {
        if (err) {
          return next(err);
        }
        if (!category) {
          return next(new Error('category not found: ', req.params.id));
        }

        req.category = category;
        next();
      });
}

